<template>
	<view class="content">
		<u-navbar leftIconColor="#000" :autoBack="true" :placeholder="true" bgColor="rgba(255,255,255,0.0)">
			<view slot="center" class="nav-title">
				订单详情
			</view>
		</u-navbar>
		<view class="order">
			<view class="order-box">
				<view class="order-info" @click="goAddress">
					<view class="order-left" v-if="addressinfo.name">
						<view class="order-head">
							<view class="name">
								{{addressinfo.name}}
							</view>
							<view class="tell">
								{{addressinfo.tel}}
							</view>
						</view>
						<view class="address">
							{{addressinfo.address_detail}}{{addressinfo.address_all}}
						</view>
					</view>
					<view v-else class="order-left">添加默认地址</view>
					<u-icon name="arrow-right" color="#333" size="19"></u-icon>
				</view>
			</view>
			<view class="order-box">
				<view class="goods">
					<view class="goods-info">
						<view class="goods-img">
							<image :src="info.logo" mode=""></image>
						</view>
						<view class="goods-explain">
							<view class="goods-name">
								{{info.goods_name}}
							</view>
							<view class="goods-price" v-if="info.class_id == 62|| info.class_id == 65 || info.class_id == 66">
								代金券：{{info.price}}
							</view>
							<view class="goods-price" v-else>
								￥{{info.price}}
							</view>
							<view class="goods-num">
								×1
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="order-box" style="margin-bottom: 0;">
				<view class="freight">
					<view>
						运费
					</view>
					<view style="color:rgba(162, 109, 71, 1);">
						免运费
					</view>
				</view>
			</view>
		</view>
		<u-modal :show="showPayModal" title="请输入支付密码" :closeOnClickOverlay="true" @cancel="closePayModal"
			:showCancelButton="true" :confirmText="'确定'" :cancelText="'取消'" @confirm="confirmPayment">
			<view class="pay-password-box">
				<u--input v-model="payPassword" type="number" placeholder="请输入6位支付密码" maxlength="6"
					:focus="showPayModal" :password="true"></u--input>
			</view>
		</u-modal>
		<view class="btn-box">
			<u-button style="color: #ffffff;" shape="circle" type="primary" color="rgba(9, 11, 8, 1)" @click="gobuy"
				text="立即购买"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info: {},
				list: [],
				addressinfo: {},
				showPayModal: false, // 控制支付弹窗显示
				payPassword: '', // 支付密码
				orderId: '' // 订单ID
			}
		},
		onLoad(e) {
			console.log(e)
			this.info = JSON.parse(decodeURIComponent(e.item))
		},
		onShow() {
			this.addressinfo = {}
			this.getaddlist()
		},
		methods: {
			getaddlist() {
				let params = {

				}
				this.$Request({
					method: 'POST',
					url: '/api/shop/addresslist',
					data: params
				}).then(res => {
					if (res.code == 1) {
						this.list = res.data
						this.list.map(item => {
							if (item.status == 1) {
								this.addressinfo = item
							}
						})
					}
				})
			},
			gobuy() {
				if (!this.addressinfo.id) {
					uni.showToast({
						title: '请选择默认地址！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					address_id: this.addressinfo.id,
					goods_id: this.info.id,
					num: 1,
				}
				if(this.info.class_id == 62|| this.info.class_id == 65 || this.info.class_id == 66){
					this.$Request({
						method: 'POST',
						url: '/api/Shop/confirmOrderDjj',
						data: params,
						ismsg: true,
					}).then(res => {
						uni.hideLoading()
						if (res.code == 1) {
							this.orderId = res.data // 保存订单ID
							this.showPayModal = true // 显示支付弹窗
						}
					})
				}else{
					this.$Request({
						method: 'POST',
						url: '/api/Shop/confirmOrder',
						data: params,
						ismsg: true,
					}).then(res => {
						uni.hideLoading()
						if (res.code == 1) {
							this.orderId = res.data // 保存订单ID
							this.showPayModal = true // 显示支付弹窗
						}
					})
				}
				
			},

			// 关闭支付弹窗
			closePayModal() {
				this.showPayModal = false
				this.payPassword = ''
			},

			// 确认支付
			confirmPayment() {
				if (!this.payPassword || this.payPassword.length !== 6) {
					uni.showToast({
						title: '请输入6位支付密码',
						icon: 'none'
					})
					return
				}

				uni.showLoading({
					title: '支付中...',
					mask: true
				})

				let params = {
					id: this.orderId,
					password: this.payPassword
				}

				this.$Request({
					method: 'POST',
					url: '/api/Shop/orderPayment',
					data: params,
					ismsg: true,
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						// 支付成功处理
						this.showPayModal = false
						this.payPassword = ''

						uni.showToast({
							title: '支付成功',
							icon: 'success'
						})

						// 跳转到订单详情或返回首页
						setTimeout(() => {
							uni.navigateBack({
								delta:1
							})
						}, 1500)
					} else {
						// 支付失败，清空密码输入框
						this.payPassword = ''
					}
				}).catch(() => {
					uni.hideLoading()
				})
			},
			goAddress() {
				uni.navigateTo({
					url: '/pages/shop/address/index'
				})
			}
		}
	}
</script>
<style>
	page {
		background-color: #f8f8f8;
	}
</style>
<style lang="scss" scoped>
	.content {
		background-image: url('../../../static/home/home-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 500rpx;
		min-height: 100vh;
		padding: 30rpx;
		.pay-password-box {
		  padding: 30rpx;
		  
		  /deep/ .u-input {
		    background-color: #f8f8f8;
		    border-radius: 10rpx;
		    padding: 20rpx;
		    text-align: center;
		    letter-spacing: 10rpx;
		    font-size: 36rpx;
		    font-weight: bold;
		  }
		}
		.btn-box {
			width: 90%;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 30rpx;
			margin: auto;
		}
		.order{
			padding: 30rpx;
			background: linear-gradient(180deg, rgba(255, 255, 255, 0.4) 0%, #FFFFFF 25.22%);
			border: 1px solid rgba(255, 255, 255, 1);
			border-radius: 20rpx;

		}
		.order-box {
			margin-bottom: 30rpx;
			.order-info {
				display: flex;
				align-items: center;
				justify-content: space-between;
			
				.order-left {
					color: #333;
					font-size: 28rpx;

					.order-head {
						display: flex;
						align-items: center;

						image {
							width: 25.53rpx;
							height: 31rpx;
						}

						.name {
							margin-left: 10rpx;
						}

						.tell {
							margin-left: 50rpx;
						}
					}

					.address {
						color: #999;
						font-size: 24rpx;
						margin-top: 15rpx;
					}
				}
			}

			.goods {
				.goods-info {
					display: flex;

					.goods-img {
						width: 150rpx;
						height: 150rpx;
						padding: 20rpx;
						text-align: center;
						border-radius: 20rpx;
						background-color: #f8f8f8;

						image {
							width: 70rpx;
							height: 120rpx;
						}
					}

					.goods-explain {
						margin-left: 20rpx;

						.goods-name {
							font-size: 28rpx;
							font-weight: bold;
							color: #000000;
						}

						.goods-price {
							margin-top: 20rpx;
							font-size: 26rpx;
							color: #FF4E4E;
						}

						.goods-num {
							margin-top: 20rpx;
							font-size: 24rpx;
							color: #999;
						}
					}
				}
			}

			.freight {
				display: flex;
				justify-content: space-between;
				font-size: 26rpx;
				color: rgba(22, 24, 31, 1);
			}
		}
	}
</style>